import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.callbacks import EarlyStopping

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
import os

(x_train, y_train), (x_test, y_test) = datasets.fashion_mnist.load_data()
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)

x_train = x_train.reshape(-1, 28*28)
x_test = x_test.reshape(-1, 28*28)

x_train = x_train/255.0
x_test = x_test/255.0

# 输入层inputx
inputs = Input(shape=(28*28), name='input')

# 隐层dense
x = Dense(units=256, activation='selu', kernel_initializer='glorot_normal', name='dense_0')(inputs)
x = Dense(units=128, activation='selu', kernel_initializer='glorot_normal', name='dense_1')(x)

# 输出层
outputs = Dense(units=10, activation='softmax', name='logit')(x)

# 设置模型的inputs和outputsin
model = Model(inputs=inputs, outputs=outputs)

# 设置损失函数loss、优化器optimizer、评价标准metrics
model.compile(loss='sparse_categorical_crossentropy',
              optimizer="sgd", metrics=['accuracy'])


model.summary()
#设定提前终止
earlyStopping = EarlyStopping(monitor='val_loss',min_delta=0,patience=10,restore_best_weights=True)

history = model.fit(x=x_train, y=y_train, batch_size=32,
                    epochs=150, validation_split=0.2,
                    shuffle=True, callbacks=[earlyStopping])
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.xlabel('epoch')
plt.show()



loss, accuracy = model.evaluate(x_test, y_test)
print('loss: ', loss)
print('accuracy: ', accuracy)

'''
不同网络的初始化方法和激活函数，和Test Accuracy的最终结果

glorot_normal   tanh    accuracy:  0.881

he_normal       relu    accuracy:  0.8877

glorot_normal   elu     accuracy:  0.8814

glorot_normal   selu    accuracy:  0.8739


'''